package com.example.teaching_server.controller;

import com.example.teaching_server.common.ApiResponse;
import com.example.teaching_server.dto.CourseRecordingDTO;
import com.example.teaching_server.service.CourseRecordingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/recordings")
@RequiredArgsConstructor
@Tag(name = "课程录播管理", description = "课程录播查询和管理接口")
public class CourseRecordingController {
    private final CourseRecordingService recordingService;

    @GetMapping
    @Operation(summary = "获取课程录播列表", description = "根据班级名称和时间范围查询课程录播信息")
    public ApiResponse<List<CourseRecordingDTO>> getRecordings(
            @Parameter(description = "班级名称，支持模糊查询")
            @RequestParam(required = false) String className,

            @Parameter(description = "开始日期，格式：yyyy-MM-dd")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,

            @Parameter(description = "结束日期，格式：yyyy-MM-dd")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {

        log.info("接收到课程录播查询请求: className={}, startDate={}, endDate={}", className, startDate, endDate);

        try {
            List<CourseRecordingDTO> recordings = recordingService.getAllRecordings(className, startDate, endDate);
            log.info("成功查询到{}条录播记录", recordings.size());
            return ApiResponse.success("课程录播获取成功", recordings);
        } catch (Exception e) {
            log.error("查询课程录播失败", e);
            return ApiResponse.fail("获取课程录播失败: " + e.getMessage());
        }
    }
}
